package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.api.BaseRest;
import com.xieyingwangluo.yxpart.bean.EnrollDTO;
import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.EnrollModel;
import com.xieyingwangluo.yxpart.entity.UserInfoModel;
import com.xieyingwangluo.yxpart.entity.WorksheetModel;
import com.xieyingwangluo.yxpart.service.EnrollService;
import com.xieyingwangluo.yxpart.service.PartService;
import com.xieyingwangluo.yxpart.service.UserInfoService;
import com.xieyingwangluo.yxpart.service.WorkService;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EnrollLogic extends BaseRest {
    @Autowired
    private EnrollService enrollService;
    @Autowired
    private WorkService workService;
    @Autowired
    private PartService partService;
    @Autowired
    private UserInfoService userInfoService;

    //添加
    @Transactional
    public Result add(EnrollDTO enrollDTO) {
        //根据兼职id查询兼职的招聘人数
       /* int hirnumb = partService.getHirnumbByNoid(enrollModel.getPart_id());

        //查询报名该兼职的人数
        List<EnrollModel> enrollModelList = enrollService.selByPartId(enrollModel.getPart_id());

        if (hirnumb == enrollModelList.size()) {
            return Result.generate(0, "该兼职已报满，请选择其他兼职进行报名吧！", null);
        }*/


        EnrollModel enrollModel = new EnrollModel();
        UserInfoModel userInfoModel = userInfoService.getByUserId(enrollDTO.getUser_id());
        enrollModel.setPart_id(enrollDTO.getPart_id());
        enrollModel.setUserinfo_id(enrollDTO.getUser_id());//userInfoModel.getNoid()
        enrollModel.setAdvantage(enrollDTO.getAdvantage());
        //根据用户id和兼职id查找报名表
        //enrollModel.getUserinfo_id()
        List<EnrollModel> list = enrollService.selUserAndPart(enrollModel.getUserinfo_id(), enrollModel.getPart_id());
        if (list.size() > 0) {
            return Result.generate(0, "Enroll already exists", null);
        }

        enrollModel.setNoid(StringBuildUtils.buildUUID());
        enrollModel.setUserinfo_id(enrollModel.getUserinfo_id());// userInfoModel.getNoid()

        WorksheetModel work = new WorksheetModel();
        int cnt = enrollService.add(enrollModel);
        if (cnt > 0) {
            work.setNoid(StringBuildUtils.buildUUID());
            work.setPart_id(enrollModel.getPart_id());
            work.setUserinfo_id(enrollModel.getUserinfo_id());
            work.setEnroll_id(enrollModel.getNoid());
            work.setRecord_time(null);
            work.setState(0);
            work.setCompletion(0);
            work.setPayamount(0);
            work.setTime(0);
            work.setMakespan(null);
            workService.add(work);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("enrollModel", enrollModel);
        map.put("work", work);
        return Result.generate(0, "Add Success", map);
    }

    //查询报名信息
    public Result selO(String noid) {
        // UserModel user = (UserModel) session.getAttribute("user");

        List<EnrollModel> list = enrollService.selO(noid);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);
        } else {
            return Result.generate(1, "Select fail", null);
        }
    }

    //模糊查询报名信息
    public Result selA(EnrollModel enrollModel) {
        List<EnrollModel> list = enrollService.selA(enrollModel);
        return Result.generate(0, "Select success", list);
    }

    //取消报名
    @Transactional
    public Result del(String noid, String part_id) {
        //根据兼职id查找工作状态（状态为已报名）
        //noid为传过来的用户id
        WorksheetModel work = workService.getByPaId(part_id);
        if (work.getState() == 0 && work.getUserinfo_id().equals(noid)) {
            int cnt = workService.del(work.getNoid());
            if (cnt > 0) {
                enrollService.del(work.getEnroll_id());
                return Result.generate(0, "Del success", cnt);
            }
        }
        return Result.generate(1, "Del fail", null);
    }
}
